/**
 * 
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.IssueTypeBOInterface;
import org.cuatrovientos.erp.model.IssueType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * IssueType Controller class
 * 
 * @author Raquel Valenzuela <raquel_valenzuela>
 * 
 */
@Controller
public class IssueTypeController {
	// Get log4j logger
	private static final Logger logger = Logger
			.getLogger(EmployeeController.class);

	@Autowired
	private IssueTypeBOInterface issueTypeBO;

	/**
	 * default constructor
	 */
	public IssueTypeController() {
		logger.info("issueType BEAN instantiated");

	}

	/**
	 * handles default /issueType or /issueType/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType","/issueType/list" )
	 */
	@RequestMapping({ "/issueType", "/issueType/list" })
	public ModelAndView issueTypeDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<IssueType> issueTypeList = issueTypeBO.selectAll();
		model.put("issueTypes", issueTypeList);
		return new ModelAndView("issueTypeList", model);
	}

	/**
	 * handles /issueType/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/issueType/search" })
	public ModelAndView search(
			@RequestParam(value = "description", defaultValue = "", required = true) String description) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<IssueType> issueTypeList = issueTypeBO.search(description);
		model.put("issueTypes", issueTypeList);

		return new ModelAndView("issueTypeList", model);
	}

	/**
	 * handles delete issueType, and redirect to employee default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType","/issueType/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issueType/delete" })
	public String deleteIssueType(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		issueTypeBO.deleteById(id);
		return "redirect:/issueType";

	}

	/**
	 * handles new issueType request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issueType/new" })
	public ModelAndView newIssueType() {
		logger.info("new issueType ");
		IssueType issueType = new IssueType();

		return new ModelAndView("IssueTypeNew", "issueType", issueType);
	}

	/**
	 * handles new issueType request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/issueType/save" })
	public String newIssueType(@Valid IssueType issueType,
			BindingResult bindingResult) {
		logger.info("new issueType " + issueType.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("issueType", issueType);
			return "IssueTypeNew";
		}

		// If data is ok, insert and go on
		issueTypeBO.insert(issueType);
		return "redirect:/issueType";
	}

	/**
	 * handles delete issueType, and redirect to issueType default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType","/issueType/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issueType/detail" })
	public ModelAndView issueTypeDetail(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		IssueType issueType = issueTypeBO.selectById(id);
		model.put("issueType", issueType);

		return new ModelAndView("IssueTypeDetail", model);

	}

	/**
	 * handles issueType update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issueType/update" })
	public ModelAndView issueTypeUpdate(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		IssueType issueType = issueTypeBO.selectById(id);
		model.put("issueType", issueType);
		return new ModelAndView("IssueTypeUpdate", model);

	}

	/**
	 * handles save update issueType request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/issueType/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/issueType/saveupdate" })
	public String saveUpdate(@Valid IssueType issueType,
			BindingResult bindingResult) {
		logger.info("update issueType " + issueType.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("issueType", issueType);
			return "IssueTypeUpdate";
		}

		issueTypeBO.update(issueType);
		return "redirect:/issueType";
	}
}
